import { ApiResponse, User, UserProfile } from '@/types/data'
import { RootThunkAction } from '@/types/store'
import request from '@/utils/request'

// 获取我的- 个人信息
export function getUser (): RootThunkAction {
  return async (dispatch) => {
    const res = await request.get<ApiResponse<User>>('/user')
    dispatch({
      type: 'profile/getUser',
      payload: res.data.data
    })
  }
}
// 获取个人信息
export const getUserProfile = (): RootThunkAction => {
  return async dispatch => {
    const res = await request.get<ApiResponse<UserProfile>>('/user/profile')
    dispatch({ type: 'profile/getUserProfile', payload: res.data.data })
  }
}
// 修改信息
export function updateUserProfile (type: string, value: string): RootThunkAction {
  return async (dispatch) => {
    await request.patch('/user/profile', {
      [type]: value
    })
    // 重新渲染
    dispatch(getUserProfile())
  }
}

export const updateUserPhoto = (data: FormData): RootThunkAction => {
  return async (dispatch) => {
    await request.patch('/user/photo', data)

    // 重新渲染
    dispatch(getUserProfile())
  }
}
